package com.ruoyi.system.controller;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WatermarkConfig;
import com.ruoyi.system.service.IWatermarkConfigService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 水印设置Controller
 *
 * @author ruoyi
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/system/WatermarkConfig")
public class WatermarkConfigController extends BaseController {
    @Autowired
    private IWatermarkConfigService watermarkConfigService;


    @GetMapping(value = "/watermarkProgramme")
    public Map<String, Object> watermarkProgramme() {
        LoginUser user = LoginHelper.getLoginUser();
//        LoginUser user = SecurityUtils.getLoginUser();
        System.out.println("用户账号：》》》》》》》》》" + user.getUsername());
        System.out.println("用户名称：》》》》》》》》》" + user.getUser().getNickName());
        System.out.println("用户手机号：》》》》》》》》》" + user.getUser().getPhonenumber());
        System.out.println("用户编号：》》》》》》》》》" + user.getUser().getUserId());
        System.out.println("用户组织：》》》》》》》》》" + user.getUser().getDept().getDeptName());

        WatermarkConfig config = watermarkConfigService.selectWatermarkConfigById(1L);
        String isAll = config.getIsall();//是否全局
        String programme = "";
        if (config.getUsercode().equals("Y")) {//是否展示用户编号
            programme += user.getUser().getUserId() + "-";
        }
        if (config.getUsername().equals("Y")) {//是否展示用户名称
            programme += user.getUser().getNickName() + "-";
        }
        if (config.getIstel().equals("Y")) {//是否展示用户手机号
            programme += user.getUser().getPhonenumber() + "-";
        }
        if (config.getWatermarkdate().equals("Y")) {//是否展示当前时间
            programme += DateUtil.today() + "-";
        }
        if (config.getOrgname().equals("Y")) {//是否展示当前组织
            programme += user.getUser().getDept().getDeptName() + "-";
        }
        programme = programme.substring(0, programme.length() - 1);
        Map<String, Object> map = new HashMap<>();
        map.put("isAll", isAll);
        map.put("programme", programme);
        return map;
    }

    /**
     * 查询水印设置列表
     */

    @GetMapping("/list")
    public TableDataInfo list(WatermarkConfig watermarkConfig, PageQuery pageQuery) {
        return watermarkConfigService.selectPageWatermarkConfigList(watermarkConfig, pageQuery);
//        startPage();
//        List<WatermarkConfig> list = watermarkConfigService.selectWatermarkConfigList(watermarkConfig);
//        return getDataTable(list);
    }

    /**
     * 导出水印设置列表
     */

    @Log(title = "水印设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(WatermarkConfig watermarkConfig, HttpServletResponse response) {
        List<WatermarkConfig> list = watermarkConfigService.selectWatermarkConfigList(watermarkConfig);
        ExcelUtil.exportExcel(list, "人员管理", WatermarkConfig.class, response);
//        ExcelUtil<WatermarkConfig> util = new ExcelUtil<WatermarkConfig>(WatermarkConfig.class);
//        return util.exportExcel(list, "WatermarkConfig");
    }

    /**
     * 获取水印设置详细信息
     */

    @GetMapping(value = "/{id}")
    public R<WatermarkConfig> getInfo(@PathVariable("id") Long id) {
        return R.ok(watermarkConfigService.selectWatermarkConfigById(id));
    }

    /**
     * 新增水印设置
     */

    @Log(title = "水印设置", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody WatermarkConfig watermarkConfig) {
        return toAjax(watermarkConfigService.insertWatermarkConfig(watermarkConfig));
    }

    /**
     * 修改水印设置
     */
    @Log(title = "水印设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody WatermarkConfig watermarkConfig) {
        return toAjax(watermarkConfigService.updateWatermarkConfig(watermarkConfig));
    }

    /**
     * 删除水印设置
     */
    @Log(title = "水印设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids) {
        return toAjax(watermarkConfigService.deleteWatermarkConfigByIds(ids));
    }
}
